package com.penjin.android.activity.secretary;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.BaseAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.easemob.util.NetUtils;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.TextHttpResponseHandler;
import com.penjin.android.R;
import com.penjin.android.constants.HttpConstants;
import com.penjin.android.http.HttpService;
import com.penjin.android.service.UserService;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONObject;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.Inflater;

/**
 * Created by lxl on 2016/4/1.
 */
public class testPushDetailsActivity  extends Activity{

    private ListView listView ;

    private List<Data> datas = new ArrayList<Data>() ;

    private Context context = this ;

    private listAdapter listAdapter = new listAdapter(this,datas) ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.test_pushlist_activity);

        listView = (ListView) findViewById(R.id.test_pushlist);

        init();

        listView.setAdapter(listAdapter);

    }


    public void init(){

        if( datas.isEmpty()){

            HttpService httpService = HttpService.getInstance(this);
            //判断网络是否正常
            if (NetUtils.hasNetwork(this)) {
                try {

                    RequestParams requestParams = new RequestParams();
                    requestParams.put("companyId", "0000000008");
                    requestParams.put("userName", "15608222863");
                    httpService.postRequest(this, HttpConstants.HOST + HttpConstants.CompanyPush, requestParams, new TextHttpResponseHandler() {
                        @Override
                        public void onFailure(int i, Header[] headers, String s, Throwable throwable) {
                            if (s == null) {
                                Toast.makeText(context, "服务器内部错误", Toast.LENGTH_SHORT).show();
                            } else {
                                System.out.println(s);
                                Toast.makeText(context, "资源路径出错", Toast.LENGTH_SHORT).show();
                            }
                        }

                        @Override
                        public void onSuccess(int i, Header[] headers, String s) {
                            try {

                                Log.d("JPush",s) ;

                                JSONObject jo = new JSONObject(s);
                                if (jo.getBoolean("result")) {
                                    JSONArray ja = jo.getJSONArray("data");

                                    Log.d("JPush",ja.toString()) ;

                                    if (ja != null && ja.length() > 0) {

                                        for( int ij = 0 ; ij < ja.length() ; ij++){

                                            JSONObject jsonObject = ja.getJSONObject(ij) ;

                                            Log.d("JPush",jsonObject.toString()) ;

                                            Data data = new Data() ;

                                            data.setContent(jsonObject.getString("picture"));
                                            data.setId(jsonObject.getString("id"));
                                            data.setTitle(jsonObject.getString("title"));

                                            datas.add(data) ;

                                        }

                                        Log.d("JPush",String.valueOf(datas.size())) ;
                                        listAdapter.notifyDataSetChanged();

                                    } else {
                                        Toast.makeText(context, "暂无数据信息", Toast.LENGTH_SHORT).show();
                                    }
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else {
                Toast.makeText(this, "网络不可用...", Toast.LENGTH_SHORT).show();
            }
        }



    }

}
